<?php
require_once("admin_global.php");    //加载后台全局调用类文件
$db->Get_user_shell_check($uid,$shell);   //后台登录控制
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>添加文章</title>
        <link rel="stylesheet" href="css/right.css" type="text/css" />
        <script type="text/javascript" src="../kindeditor/kindeditor.js"></script>
        <script type="text/javascript" src="../kindeditor/lang/zh_CN.js"></script>
        <script>
            KindEditor.ready(function(K) {
                var editor = K.create('textarea[name="content"]', {
                    cssPath : '../kindeditor/plugins/code/prettify.css',
                    uploadJson : '../kindeditor/php/upload_json.php',
                    fileManagerJson : '../kindeditor/php/file_manager_json.php',
                    allowFileManager : true,
                    afterBlur: function(){this.sync();},              //编辑器失去焦点后同步KindEditor的值到textarea文本框，这一行很重要。
                    afterCreate : function() {
                        var self = this;
                        K.ctrl(document, 13, function() {
                            self.sync();
                            K('form[name=example]')[0].submit();
                        });
                        K.ctrl(self.edit.doc, 13, function() {
                            self.sync();
                            K('form[name=example]')[0].submit();
                        });
                    }
                });
                prettyPrint();
            });
        </script>
        <script>
            window.onload=function () {
                var myform=document.getElementById('form1');
                myform.onsubmit=function(){
                    if(myform.newsTitle.value==""){
                        alert('请输入文章标题！');
                        myform.newsTitle.focus();
                        return false;
                    }
                    if(myform.newsAuthor.value==""){
                        alert('请输入文章作者！');
                        myform.newsAuthor.focus();
                        return false;
                    }
                    if(myform.content.value==""){
                        alert('请输入文章内容！');
                        myform.content.focus();
                        return false;
                    }
                    if(myform.pid.value==0){
                        alert('请选择栏目！');
                        return false;
                    }
                }
            }
        </script>
    </head>
    <?php
    //查找出已有栏目并存储到数组中
    $sql="SELECT * FROM category";
    $query=$db->query($sql);
    while($row=$db->fetch_array($query)){
        $data[]=$row;
    }
    //实例化一个树形类对象
    $tree=new Tree($data);
    //获取树形栏目结构
    $gettree=$tree->get_tree(0,"<option value=\$id >\$spacer\$cateName</option>",0);
   //准备写入数据库
    if(isset($_POST['addNews'])){
        $cateID=$_POST['pid'];  //获取栏目ID
        //判断该栏目下是否有子栏目
        $sql="SELECT * FROM category WHERE catePID=$cateID";
        $query=$db->query($sql);
        $num=$db->db_num_rows($query);
        if($num<=0){
            //判断该栏目是否为单页栏目
            $sql="SELECT * FROM category WHERE id=$cateID";
            $query=$db->query($sql);
            $result=$db->fetch_array($query);
            if($result['cateStyleID']<>2){
                //获取表单提交的数据
                $newsTitle=$_POST['newsTitle'];
                $newsKeyWords=$_POST['newsKeyWords'];
                $newsAuthor=$_POST['newsAuthor'];
                $newsSource=$_POST['newsSource'];
                if(!empty($_POST['isTop'])){
                    $isTop=1;
                }else{
                    $isTop=0;
                }
                if(!empty($_POST['isRed'])){
                    $isRed=1;
                }else{
                    $isRed=0;
                }
                $newsSendTime=time();
                $newsUpdateTime=$newsSendTime;
                $newsContentTMP=$_POST['content'];
                $patten = '/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i';
                if(preg_match($patten,$newsContentTMP,$matche)){
                    $newsImagePath=$matche[1];
                }else{
                    $newsImagePath="";
                }
                if(!get_magic_quotes_gpc()){
                    $newsContent=addslashes($_POST['content']);
                }else{
                    $newsContent=$_POST['content'];
                }
                //写入数据库
                $sql="INSERT INTO news(newsID,newsCID,newsAuthor,newsTitle,newsSource,isTop,isRed,newsSendTime,newsUpdateTime,newsImagePath,newsKeyWords,newsContent) VALUES(null,$cateID,'$newsAuthor','$newsTitle','$newsSource',$isTop,$isRed,$newsSendTime,$newsUpdateTime,'$newsImagePath','$newsKeyWords','$newsContent')";
                $query=$db->query($sql);
                if($query==true){
                    //根据管理员类型跳转到相应的链接
                    if($_SESSION['userType']=='普通管理员'){
                        echo "<script>alert('文章添加成功！');location.href='listMyNews.php';</script>";
                    }else{
                        echo "<script>alert('文章添加成功！');location.href='listNews.php';</script>";
                    }
                }
            }else{
                echo "<script>alert('不能选择单页栏目！');history.go(-1);</script>";
            }
        }else{
            echo "<script>alert('该栏目下有子栏目，不能选择！');history.go(-1);</script>";
        }
    }

    ?>
    <body>
        <div id="right_main">
            <div class="right_content">
                <div class="right_top_menu">
                    <h3>
                        您当前的位置：后台 > 文章管理 > 添加文章
                    </h3>
                </div>
                <div class="right_center_content">
                    <form id="form1" method="post" action="">
                        <table cellpadding="0" cellspacing="0" border="0" width="100%">
                            <tr>
                                <td valign="top" >
                                    <div class="txl_search"></div>
                                    <table cellpadding="0" cellspacing="0" border="0" width="100%"  class="sy_table">
                                        <tr>
                                            <th colspan="2" >添加文章</th>
                                        </tr>
                                        <tr>
                                            <td width="10%" align="right">所属栏目：</td>
                                            <td width="90%">
                                                <select name="pid">
                                                    <option value="0">请选择文章所属栏目</option>
                                                    <?php print_r($gettree); ?>
                                                </select> *不能指定为含有子栏目的栏目！
                                            </td>
                                        </tr> 
                                        <tr>
                                            <td width="10%" align="right">文章标题：</td>
                                            <td width="90%"><input type="text" name="newsTitle"  class="int_text" value="" size="60"  maxlength="60"/> *文章标题不能少于6个字符！</td>
                                        </tr> 
                                        <tr>
                                            <td width="10%" align="right">关键字：</td>
                                            <td width="90%"><input type="text" name="newsKeyWords"  class="int_text" value="" size="40"  maxlength="40"/></td>
                                        </tr> 
                                        <tr>
                                            <td width="10%" align="right">文章作者：</td>
                                            <td width="90%"><input type="text" name="newsAuthor"  class="int_text" value="" size="40"  maxlength="40"/> *必填</td>
                                        </tr> 
                                        <tr>
                                            <td width="10%" align="right">文章来源：</td>
                                            <td width="90%"><input type="text" name="newsSource"  class="int_text" value="" size="40"  maxlength="40"/></td>
                                        </tr>
                                        <tr>
                                            <td width="10%" align="right">是否置顶：</td>
                                            <td width="90%"><input type="checkbox" name="isTop"  class="int_text" value="6" size="40" /></td>
                                        </tr>
                                        <tr>
                                            <td width="10%" align="right">是否加红：</td>
                                            <td width="90%"><input type="checkbox" name="isRed"  class="int_text" value="6" size="40" /></td>
                                        </tr>   
                                        <tr>
                                            <td width="10%" align="right">文章内容：</td>
                                            <td width="90%"><textarea id="editor1" class=".int_area" name="content" style="width: 95%;height:400px;"></textarea></td>
                                        </tr> 
                                        <tr>
                                            <td colspan="2" align="center" height='30'>
                                                <input type="submit" name="addNews" class="btn_ipt01" value=" 添加文章 "/>
                                            </td>  
                                        </tr>
                                    </table>
                                </td>
                            </tr>   
                        </table>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>
